home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 1092 / 1092.xpi / chrome / dotcomplete.jar / content / dotcomplete / dotcomplete.js < prev    next >
Text File  |  2006-11-10  |  13KB  |  270 lines

  1. var dotCOMplete =
  2. {
  3.   TLDList: [
  4.     // All active country code- and generic- top level domains.
  5.     // Taken from http://data.iana.org/TLD/tlds-alpha-by-domain.txt
  6.     // Version 2006101901, Last Updated Fri Oct 20 08:07:01 2006 UTC
  7.     "ac","ad","ae","aero","af","ag","ai","al","am","an","ao","aq","ar","arpa","as","at",
  8.     "au","aw","ax","az","ba","bb","bd","be","bf","bg","bh","bi","biz","bj","bm","bn",
  9.     "bo","br","bs","bt","bv","bw","by","bz","ca","cat","cc","cd","cf","cg","ch","ci",
  10.     "ck","cl","cm","cn","co","com","coop","cr","cu","cv","cx","cy","cz","de","dj","dk",
  11.     "dm","do","dz","ec","edu","ee","eg","er","es","et","eu","fi","fj","fk","fm","fo",
  12.     "fr","ga","gb","gd","ge","gf","gg","gh","gi","gl","gm","gn","gov","gp","gq","gr",
  13.     "gs","gt","gu","gw","gy","hk","hm","hn","hr","ht","hu","id","ie","il","im","in",
  14.     "info","int","io","iq","ir","is","it","je","jm","jo","jobs","jp","ke","kg","kh",
  15.     "ki","km","kn","kr","kw","ky","kz","la","lb","lc","li","lk","lr","ls","lt","lu",
  16.     "lv","ly","ma","mc","md","mg","mh","mil","mk","ml","mm","mn","mo","mobi","mp","mq",
  17.     "mr","ms","mt","mu","museum","mv","mw","mx","my","mz","na","name","nc","ne","net",
  18.     "nf","ng","ni","nl","no","np","nr","nu","nz","om","org","pa","pe","pf","pg","ph",
  19.     "pk","pl","pm","pn","pr","pro","ps","pt","pw","py","qa","re","ro","ru","rw","sa",
  20.     "sb","sc","sd","se","sg","sh","si","sj","sk","sl","sm","sn","so","sr","st","su",
  21.     "sv","sy","sz","tc","td","tf","tg","th","tj","tk","tl","tm","tn","to","tp","tr",
  22.     "travel","tt","tv","tw","tz","ua","ug","uk","um","us","uy","uz","va","vc","ve","vg",
  23.     "vi","vn","vu","wf","ws","ye","yt","yu","za","zm","zw"
  24.   ],
  25.  
  26.   preferences: Components.classes["@mozilla.org/preferences-service;1"]
  27.                  .getService(Components.interfaces.nsIPrefService).getBranch("dotCOMplete."),
  28.  
  29.   compatTBPrefs: false,
  30.  
  31.   initDotCOMplete: function()
  32.   {
  33.     setTimeout(window.dotCOMplete.delayedInitDotCOMplete,1);
  34.   },
  35.  
  36.   delayedInitDotCOMplete: function()
  37.   {
  38.     if ((typeof(window.gURLBar) == "object") && window.gURLBar &&
  39.         window.handleURLBarCommand && (window.dotCOMplete.orighandleURLBarCommand == null))
  40.     {
  41.       // Replace original handleURLBarCommand, or TBP_handleURLBarCommand in case
  42.       // Tabbrowser Preferences extension v1.3.1.* (and up, hopefully) is installed
  43.       if (window.dotCOMplete.compatTBPrefs = (
  44.             window.TBP_handleURLBarCommand && (typeof(window.TBP_handleURLBarCommand) == 'function') &&
  45.             window.TBP_BrowserLoadURL && (typeof(window.TBP_BrowserLoadURL) == 'function')
  46.          ))
  47.       {
  48.         window.dotCOMplete.realBrowserLoadURL = window.TBP_BrowserLoadURL;
  49.         window.dotCOMplete.orighandleURLBarCommand = window.TBP_handleURLBarCommand;
  50.         window.TBP_handleURLBarCommand = window.dotCOMplete.handleURLBarCommand;
  51.       }
  52.       else
  53.       {
  54.         window.dotCOMplete.realBrowserLoadURL = window.BrowserLoadURL;
  55.         window.dotCOMplete.orighandleURLBarCommand = window.handleURLBarCommand;
  56.         window.handleURLBarCommand = window.dotCOMplete.handleURLBarCommand;
  57.       }
  58.       Components.classes['@mozilla.org/consoleservice;1']
  59.           .getService(Components.interfaces.nsIConsoleService)
  60.           .logStringMessage('dotCOMplete: Tabbrowser Preferences compatability mode: '+
  61.                             (window.dotCOMplete.compatTBPrefs ? 'enabled' : 'disabled'));
  62.  
  63.       // Cache list of active TLD's into an object for fast access
  64.       var oTLDs = {};
  65.       oTLDs.prototype = null;
  66.       for(var iCnt = window.dotCOMplete.TLDList.length; iCnt--;)
  67.         oTLDs[window.dotCOMplete.TLDList[iCnt]] = 0;
  68.       window.dotCOMplete._TLDList = oTLDs;
  69.  
  70.     }
  71.   },
  72.  
  73.   orighandleURLBarCommand: null,
  74.   handleURLBarCommand: function(aTriggeringEvent)
  75.   {
  76.     // This code is based on chrome://browser/content/browser.js and should provide identical functionality
  77.     // except for using dotCOMplete's version of canonizeUrl and the delayed loading in case a completion
  78.     // key combination was used. Based on the Firefox 1.6a version of handleURLBarCommand which is backwards
  79.     // compatible with the Firefox 1.0.
  80.  
  81.     if (window.dotCOMplete._timer)
  82.     {
  83.       try { clearTimeout(window.dotCOMplete._timer); } catch(vErr) { /* ignore */ }
  84.       delete window.dotCOMplete._timer;
  85.       delete window.dotCOMplete._triggeringEvent;
  86.       delete window.dotCOMplete._URL;
  87.     }
  88.  
  89.     var postData = { };
  90.     var iAction = window.dotCOMplete.canonizeUrl(aTriggeringEvent, postData);
  91.     if (window.dotCOMplete.compatTBPrefs && (aTriggeringEvent.type == "click") && (aTriggeringEvent.button == 0))
  92.       return;  // This is what TBPrefs v1.3.1.1 does
  93.  
  94.     switch(iAction) {
  95.       case 1:  /* open immediately */
  96.         try { addToUrlbarHistory(gURLBar.value); } catch(vErr) { /* ignore */ }
  97.         window.dotCOMplete.realBrowserLoadURL(aTriggeringEvent,postData.value);
  98.         break;
  99.  
  100.       case 2:  /* open delayed */
  101.         window.dotCOMplete._URL = gURLBar.value;
  102.         window.dotCOMplete._triggeringEvent = {};
  103.         for(var sKey in aTriggeringEvent)
  104.           window.dotCOMplete._triggeringEvent[sKey] = aTriggeringEvent[sKey];
  105.         window.dotCOMplete._timer = setTimeout(window.dotCOMplete.BrowserLoadURL, window.dotCOMplete.preferences.getIntPref("loadDelay"));
  106.     }
  107.   },
  108.  
  109.   canonizeUrl: function(aTriggeringEvent, aPostDataRef)
  110.   {
  111.     try
  112.     {
  113.       if (!(gURLBar && aTriggeringEvent))
  114.         throw "no completion requested";
  115.  
  116.       // Fetch the optional completion information to apply
  117.       var iCompletion =
  118.         (('ctrlKey' in aTriggeringEvent) && ('shiftKey' in aTriggeringEvent) &&
  119.          (aTriggeringEvent.ctrlKey || aTriggeringEvent.shiftKey))
  120.         ? (aTriggeringEvent.ctrlKey ? 1 : 0) + (aTriggeringEvent.shiftKey ? 2 : 0) - 1
  121.         : 3;
  122.       var oPrefs = window.dotCOMplete.preferences;
  123.       var sDefaultScheme = oPrefs.getCharPref(iCompletion+".scheme");
  124.       var bForceScheme = oPrefs.getBoolPref(iCompletion+".forceScheme");
  125.       var sPrefix = oPrefs.getCharPref(iCompletion+".FQNprefix");
  126.       var iMinParts = oPrefs.getIntPref(iCompletion+".FQNminparts");
  127.       var sTLDs = oPrefs.getCharPref(iCompletion+".FQNsuffixes");
  128.  
  129.       // Test if completion is enabled, otherwise apply FF default loading mechanism
  130.       if ((sDefaultScheme == "") && (iMinParts <= 1) && (sTLDs == ""))
  131.         throw "no completion requested";
  132.  
  133.       // Extract and correct the URL parts
  134.       var sURL = (new String(gURLBar.value)).replace(/^\s+/,"");
  135.       if (!/^\s*([^:]+:+\/*)?([^@\/]*@+)?((?:[^.\/:]*\.?)+)?(.*?)?\s*$/.test(sURL)) //*/
  136.         throw "unparseable URL";  // This should never be the case
  137.       var sScheme = RegExp.$1, sCredentials = RegExp.$2, sFQN = RegExp.$3, sPath = RegExp.$4;
  138.       sScheme = sScheme.replace(/\s+/g,"").replace(/:{2,}/g,":").replace(/\/{3,}$/,"//");
  139.       sFQN = sFQN.replace(/\s+/g,"").replace(/\.{2,}/g,".").replace(/^\.|\.$/,"").toLowerCase();
  140.       sPath = sPath.replace(/^\/{2,}/g,"/");
  141.       //if (!/[^.]{2,}/.test(sFQN))
  142.       //  return 0;  /* cancelled */
  143.  
  144.       // Set the default scheme if not specified or different from forced scheme
  145.       var bIncorrectScheme = ((sDefaultScheme != "") && ((sScheme == "") || (bForceScheme && (sScheme != sDefaultScheme))));
  146.       if (bIncorrectScheme)
  147.         sScheme = sDefaultScheme;
  148.  
  149.       // Find the current TLD and the one to cycle to
  150.       var iFQNLength = sFQN.length, sCurrentTLD = "", sNextTLD = "";
  151.       if (sTLDs != "")
  152.       {
  153.         var aTLD = sTLDs.toLowerCase().split(",");
  154.         aTLD.push(sNextTLD = aTLD[0]);
  155.         for(var sTLD; sTLD = aTLD.shift();)
  156.           if (sFQN.substr(iFQNLength - sTLD.length) == sTLD)
  157.           {
  158.             sFQN = sFQN.substr(0,iFQNLength - sTLD.length);
  159.             sCurrentTLD = sTLD;
  160.             sNextTLD = aTLD.shift();
  161.             break;
  162.           }
  163.       }
  164.       if (sCurrentTLD == "")
  165.       {
  166.         for(var sList, iCnt=3; iCnt--;)
  167.           if ((iCnt != iCompletion) && ((sList = oPrefs.getCharPref(iCnt+".FQNsuffixes").toLowerCase()) != ""))
  168.             for(var aTLD=sList.split(","), sTLD; sTLD = aTLD.shift();)
  169.               if (sFQN.substr(iFQNLength - sTLD.length) == sTLD)
  170.               {
  171.                 sFQN = sFQN.substr(0,iFQNLength - sTLD.length);
  172.                 sCurrentTLD = sTLD;
  173.                 break;
  174.               }
  175.         if ((sCurrentTLD == "") && /\.([^.]+)$/.test(sFQN) &&
  176.             (RegExp.$1 in window.dotCOMplete._TLDList))
  177.         {
  178.           sFQN = sFQN.substr(0,iFQNLength - RegExp.$1.length - 1);
  179.           sCurrentTLD = "." + RegExp.$1;
  180.         }
  181.       }
  182.  
  183.       // Precede the FQN with missing parts if less than the default minimum parts
  184.       var iNrFQNParts = (sFQN == "" ? 0 : sFQN.split(/\./).length) + 1 /* TLD is one already */
  185.       var bPartsAdded = false;
  186.       if (iNrFQNParts < iMinParts)
  187.       {
  188.         var aDefaultParts = sPrefix.split(/\./);
  189.         aDefaultParts.splice(iMinParts - iNrFQNParts);
  190.         var aPartsToInsert = [];
  191.         while(aDefaultParts.length && (sFQN+".").indexOf(aDefaultParts.join(".") + "."))
  192.           aPartsToInsert.push(aDefaultParts.shift());
  193.         if (aPartsToInsert.length)
  194.         {
  195.           iNrFQNParts += aPartsToInsert.length;
  196.           sFQN = aPartsToInsert.join(".") + "." + sFQN;
  197.           bPartsAdded = true;
  198.         }
  199.       }
  200.  
  201.       // Complete the FQN with TLD
  202.       if (bIncorrectScheme || bPartsAdded)  // Keep current TLD or select first TLD when other completions occurred
  203.       {
  204.         if (sCurrentTLD != "")
  205.           sFQN += sCurrentTLD;
  206.         else if (iNrFQNParts <= iMinParts)
  207.           sFQN += sNextTLD;
  208.       }
  209.       else if ((sCurrentTLD != "") || (sNextTLD != ""))  // Otherwise cycle next TLD
  210.         sFQN += sNextTLD;
  211.  
  212.       // Formulate completed URL
  213.       gURLBar.value = getShortcutOrURI(sScheme + sCredentials + sFQN + sPath, aPostDataRef);
  214.       return 2;  /* open delayed */
  215.     }
  216.     catch(vErr)
  217.     {
  218.       window.canonizeUrl(aTriggeringEvent, aPostDataRef);
  219.       return 1;  /* open immediately */
  220.     }
  221.   },
  222.  
  223.   realBrowserLoadURL: null,
  224.   BrowserLoadURL: function()
  225.   {
  226.     window.dotCOMplete._timer = null;
  227.     if (window.dotCOMplete._URL == gURLBar.value)
  228.     {
  229.       try { addToUrlbarHistory(gURLBar.value); } catch(vErr) { /* ignore */ }
  230.       window.dotCOMplete.realBrowserLoadURL(window.dotCOMplete._triggeringEvent, null);
  231.     }
  232.     delete window.dotCOMplete._URL;
  233.     delete window.dotCOMplete._triggeringEvent;
  234.   },
  235.  
  236.   initOptions: function(oDialog)
  237.   {
  238.     var oD = document, oPrefs = window.dotCOMplete.preferences;
  239.     oD.getElementById("loadDelay").value = oPrefs.getIntPref("loadDelay");
  240.     for(var iCnt=4; iCnt--;) {
  241.       oD.getElementById("scheme"+iCnt).value = oPrefs.getCharPref(iCnt+".scheme");
  242.       oD.getElementById("forceScheme"+iCnt).checked = oPrefs.getBoolPref(iCnt+".forceScheme");
  243.       oD.getElementById("FQNprefix"+iCnt).value = oPrefs.getCharPref(iCnt+".FQNprefix");
  244.       oD.getElementById("FQNminparts"+iCnt).value = oPrefs.getIntPref(iCnt+".FQNminparts");
  245.       oD.getElementById("FQNsuffixes"+iCnt).value = oPrefs.getCharPref(iCnt+".FQNsuffixes")
  246.                                                       .replace(/^[.,]+/,"").replace(/,\.*/g,"\n");
  247.     }
  248.     oD.getElementById("hotKeys").selectedIndex = oD.getElementById("hotKeysDeck").selectedIndex;
  249.   },
  250.  
  251.   applyOptions: function()
  252.   {
  253.     var oD = document, oPrefs = window.dotCOMplete.preferences;
  254.     oPrefs.setIntPref("loadDelay", parseInt("0"+oD.getElementById("loadDelay").value,10));
  255.     for(var iCnt=4; iCnt--;) {
  256.       var sScheme = oD.getElementById("scheme"+iCnt).value;
  257.       oPrefs.setCharPref(iCnt+".scheme", sScheme + ((sScheme!="" && sScheme.indexOf(":")==-1) ? ":" : ""));
  258.       oPrefs.setBoolPref(iCnt+".forceScheme", oD.getElementById("forceScheme"+iCnt).checked);
  259.       oPrefs.setCharPref(iCnt+".FQNprefix", oD.getElementById("FQNprefix"+iCnt).value);
  260.       oPrefs.setIntPref(iCnt+".FQNminparts", parseInt("0"+oD.getElementById("FQNminparts"+iCnt).value,10));
  261.       var sSuffixes = oD.getElementById("FQNsuffixes"+iCnt).value
  262.                         .replace(/[ \t]+/g,"").replace(/\.{2,}/g,".")
  263.                         .replace(/(?:\.*[\r\n,:\/@]\.*)+/g,",.").replace(/[,.]+$/,"");
  264.       oPrefs.setCharPref(iCnt+".FQNsuffixes", sSuffixes=="" ? "" : "."+sSuffixes);
  265.     }
  266.   }
  267. };
  268.  
  269. window.addEventListener('load',dotCOMplete.initDotCOMplete,false);
  270.